package com.elitel.frame.task;

import com.elitel.frame.base.service.CfgDbService;
import com.elitel.frame.base.service.CfgTableService;
import com.elitel.frame.business.service.DataBaseMetadataService;
import com.elitel.frame.main.entity.CfgDb;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Component("refreshTableCountScheduler")
public class RefreshTableCountScheduler {
    private static final Logger logger = LoggerFactory.getLogger(RefreshTableCountScheduler.class);
    private final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private CfgDbService cfgDbService;
    @Autowired
    private DataBaseMetadataService dataBaseMetadataService;
    @Autowired
    private CfgTableService cfgTableService;

    public void refreshCfgTableCount() {
        logger.info("======= 开始刷新配置表数据统计信息(refreshCfgTableCount)! 时间: " + df.format(System.currentTimeMillis()));
        List<CfgDb> allListCfgDb = cfgDbService.getAllListCfgDb();
        for (CfgDb cfgDb : allListCfgDb) {
            List<Map<String, Object>> dbTableRecordCount = dataBaseMetadataService.getDBTableRecordCount(cfgDb, null);
            cfgTableService.refreshTableCount(cfgDb.getDbKey(), dbTableRecordCount);
        }

        logger.info("刷新配置表数据统计信息成功！时间: " + df.format(System.currentTimeMillis()));
    }
}
